package top.so.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.BlogArticleDTO;
import top.so.dto.CommentDTO;
import top.so.dto.UserDTO;
import top.so.entity.Admin;
import top.so.service.AdminService;
import top.so.service.BlogArticleService;
import top.so.service.CommentService;
import top.so.service.UserService;

import java.util.List;

@Controller
@RequestMapping("/Management")
public class ManagementController {

    @Autowired
    private UserService userService;
    @Autowired
    private BlogArticleService blogArticleService;
    @Autowired
    private CommentService commentService;

    @Autowired
    private AdminService adminService;
    @RequestMapping("")
    public ModelAndView Managementlogin(){
        return new ModelAndView("main/managementlogin");
    }
    @RequestMapping(value = "login",method = {RequestMethod.POST})
    public ModelAndView login(Admin admin, ModelMap modelMap){
        ModelAndView mv = new ModelAndView();
        List<Admin> adminList= adminService.selectAdmin(admin);
//        List<UserDTO> userDTOList =  userService.selectUser(admin);
        //判断结果集中是否有匹配的数据 如有则返回到主页
        if(admin.getPassword()!=""&& admin.getLoginName()!=""){
            for(Admin admin1:adminList){
                List<UserDTO> userDTOList=userService.selectUser(new UserDTO());
                mv.addObject("userDTOList",userDTOList);
                mv.setViewName("main/managementPage");
                return mv;
//                List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(new BlogArticleDTO(),1);
//                mv.addObject("blogArticleDTOList",blogArticleDTOList);
//                mv.setViewName("main/managementPage");
//                return mv;
            }
        }else{
            //否则 提示账号或密码错误
            mv.addObject("wrongInfo","账号或密码不能为空!");
            mv.setViewName("main/managementlogin");
            return mv;
        }
        //否则 提示账号或密码错误
        mv.addObject("wrongInfo","账号或密码错误!");
        mv.setViewName("main/managementlogin");
        return mv;
    }
    //查看用户
    @RequestMapping(value = "users")
    public ModelAndView users(){
        ModelAndView mv = new ModelAndView();
        List<UserDTO> userDTOList=userService.selectUser(new UserDTO());
        mv.addObject("userDTOList",userDTOList);
        mv.setViewName("main/managementPage");
        return mv;
    }
    //查看文章
    @RequestMapping(value = "article")
    public ModelAndView article(){
        ModelAndView mv = new ModelAndView();
        List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(new BlogArticleDTO(),1);
        mv.addObject("blogArticleDTOList",blogArticleDTOList);
        mv.setViewName("main/managementPageArticle");
        return mv;
    }

    //查看评论
    @RequestMapping(value = "comment")
    public ModelAndView comment(){
        ModelAndView mv = new ModelAndView();
        List<CommentDTO>commentDTOList=commentService.selectCommentWithPage(new CommentDTO(),1);
        System.out.println("*************************************");
        for(CommentDTO commentDTO:commentDTOList){
            System.out.print(commentDTO.getArticleID());
            System.out.print(commentDTO.getCommentContent());
            System.out.print(commentDTO.getCommentDate());
            System.out.print(commentDTO.getCommentID());
            System.out.println(commentDTO.getUserName());
        }
        System.out.println("*************************************");
        mv.addObject("commentDTOList",commentDTOList);
        mv.setViewName("main/managementPageComments");
        return mv;
    }

    //删除评论
    @RequestMapping(value = "deleteComment",method = RequestMethod.POST)
    public ModelAndView deleteComment(CommentDTO commentDTO, ModelMap modelMap){
        ModelAndView mv = new ModelAndView("main/managementPageComments");
        if(commentService.deleteComment(commentDTO)){
            List<CommentDTO> commentDTOList=commentService.selectCommentWithPage(new CommentDTO(),1);
            mv.addObject("commentDTOList",commentDTOList);
            modelMap.addAttribute("isSuccess","true");
        }
        else{
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }
    //删除文章
    @RequestMapping(value = "deleteArticle",method = RequestMethod.POST)
    public ModelAndView deleteArticle(BlogArticleDTO blogArticleDTO, ModelMap modelMap){
        ModelAndView mv = new ModelAndView("main/managementPageArticle");
        if(blogArticleService.deleteBlogArticle(blogArticleDTO)){
            List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(new BlogArticleDTO(),1);
            mv.addObject("blogArticleDTOList",blogArticleDTOList);
            modelMap.addAttribute("isSuccess","true");
        }
        else{
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }
    //删除用户
    @RequestMapping(value = "deleteUser",method = RequestMethod.POST)
    public ModelAndView deleteUser(UserDTO userDTO,ModelMap modelMap){
        ModelAndView mv=new ModelAndView("main/managementPage");
        if(userService.deleteUser(userDTO)){
            List<UserDTO> userDTOList=userService.selectUser(new UserDTO());
            mv.addObject("userDTOList",userDTOList);
            modelMap.addAttribute("isSuccess","true");
        }else {
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }

//    //删除评论
//    @RequestMapping(value = "deleteArticle",method = RequestMethod.POST)
//    public ModelAndView deleteArticle(BlogArticleDTO blogArticleDTO, ModelMap modelMap){
//        ModelAndView mv = new ModelAndView("main/managementPage");
//        if(blogArticleService.deleteBlogArticle(blogArticleDTO)){
//            List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(new BlogArticleDTO(),1);
//            mv.addObject("blogArticleDTOList",blogArticleDTOList);
//            modelMap.addAttribute("isSuccess","true");
//        }
//        else{
//            modelMap.addAttribute("isSuccess","false");
//        }
//        return mv;
//    }

//    //删除用户
//    @RequestMapping(value = "deleteArticle",method = RequestMethod.POST)
//    public ModelAndView deleteArticle(BlogArticleDTO blogArticleDTO, ModelMap modelMap){
//        ModelAndView mv = new ModelAndView("main/managementPage");
//        if(blogArticleService.deleteBlogArticle(blogArticleDTO)){
//            List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(new BlogArticleDTO(),1);
//            mv.addObject("blogArticleDTOList",blogArticleDTOList);
//            modelMap.addAttribute("isSuccess","true");
//        }
//        else{
//            modelMap.addAttribute("isSuccess","false");
//        }
//        return mv;
//    }

    @RequestMapping(value = "goPage",method = RequestMethod.POST)
    public ModelAndView goPage(int pageNum){
        ModelAndView mv = new ModelAndView("main/managementPageArticle");
        BlogArticleDTO blogArticleDTO = new BlogArticleDTO();
        List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectBlogArticleWithPage(blogArticleDTO,pageNum);
        mv.addObject("blogArticleDTOList",blogArticleDTOList);
        return mv;
    }

}
